package org.teamh.gui.engine.ogg;

public class Audio {
	/** The buffer containing the sound */
	private int buffer;

	private int index;

	private float volume = 1.0f;
	private boolean loop;
	private float pitch;
	private float[] pos;
	private float[] vel;

	public Audio(int buffer) {
		this.buffer = buffer;
	}

	/**
	 * Set the sound volume
	 * 
	 * @param volume
	 *            The volume for sound fx
	 */
	public void setVolume(float volume) {

		this.volume = volume;
		SoundCache.get().setVolume(volume, index);

	}

	/**
	 * Get the volume for sounds
	 * 
	 * @return The volume for sounds
	 */
	public float getVolume() {

		return volume;

	}

	public void setLoop(boolean loop) {

		this.loop = loop;
		SoundCache.get().setLoop(loop, index);

	}

	public boolean getLoop() {

		return loop;

	}

	/**
	 * Set the pitch at which the current music is being played
	 * 
	 * @param pitch
	 *            The pitch at which the current music is being played
	 */
	public void setPitch(float pitch) {

		this.pitch = pitch;
		SoundCache.get().setPitch(pitch, index);

	}

	public float getPitch() {

		return pitch;

	}

	public void setSourcePosition(float x, float y, float z) {

		setSourcePosition(new float[] { x, y, z });

	}

	public void setSourcePosition(float[] pos) {

		this.pos = pos;

		SoundCache.get().setSourcePosition(pos, index);

	}

	public float[] getSourcePostion(int idx) {

		return pos;

	}

	public void setVelocity(float x, float y, float z) {

		setVelocity(new float[] { x, y, z });

	}

	public void setVelocity(float[] vel) {

		this.vel = vel;

		SoundCache.get().setSourceVelocity(vel, index);

	}

	public float[] getSourceVelocity() {

		return vel;

	}

	public void play() {

		SoundCache.get().play(index);

	}

	public void play(float pitch, float volume, boolean loop, float x, float y,
			float z) {

		this.volume = volume;
		this.pitch = pitch;
		this.loop = loop;

		pos = new float[] { x, y, z };

		index = SoundCache.get().findFreeSource();

		SoundCache.get().play(buffer, pitch, volume, loop, index, x, y, z);

	}

	public void stop() {

		SoundCache.get().stop(index);

	}

	public void pause() {

		SoundCache.get().pause(index);

	}

	public boolean isPlaying() {
		return SoundCache.get().isPlaying(index);
	}

	public Audio clone() {
		return new Audio(buffer);
	}
	
	public int getIndex() {
		return index;
	}
}
